#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/10/26 15:03
# @Author  : supuchun
# @Site    : 
# @File    : read_file_demo.py

from multiprocessing import Pool
import time
import os,requests
import pymysql

def open_db():
    global db, cursor
    db = pymysql.connect("rm-2ze108d8naeso02i1o.mysql.rds.aliyuncs.com", "elephant", "K6hd562P1m", "compare_v2",charset='utf8')
    cursor = db.cursor()

def close_db():
    global db
    db.close()
def get_data():
    global db, cursor
    company_id = 194
    spider_id = 1
    sql = "select company_id,id,oss_url from compare_100004 where size > 30 and width/height >= 0.3062 and width/height <= 3.26 and img_format < 3 "
    #sql ="""select oss_url,clue_id,task_id,pic_url from compare_v2.compare_%s where id =%s """ % (company_id,spider_id)
    try:
        # 执行sql语句
        cursor.execute(sql)
        # 提交到数据库执行
        db.commit()
        result = cursor.fetchAll()
    except:
        # 如果发生错误则回滚
        #print(sql)

        db.rollback()
        return False
    return result
def fo_data():
    open_db()
    data = get_data()
    #print(data)
    for i in data:
        oss_url = "http://elephant-spider.vpc100-oss-cn-beijing.aliyuncs.com/"+i['oss_url']+"?x-oss-process=image/resize,m_lfit,h_400,w_400"
        url = "http://172.16.237.71/getfeatures?ossurl="+oss_url
        feature = requests.get(url)
        dd = "100004-"+i['id']+"   "+feature
        sayHi(dd)

def mycallback(x):
    with open('123.txt', 'a+') as f:
        f.writelines(str(x))

def write_data(data):
    with open ('1.csv','a',charset='utf-8') as f:
        f.write(data+"\n")

def sayHi(dd):
    return dd


def start():
    fo_data()
    e1 = time.time()
    pool = Pool()

    for i in range(10):
        pool.apply_async(sayHi, callback=mycallback)

    pool.close()
    pool.join()
    e2 = time.time()
    print(float(e2 - e1))

start()